package csel.view;
import java.awt.Graphics;
import java.awt.Point;

import csel.model.*;
/**
 * Renders the tiles on a map
 * @author Ryan
 *
 */
public class TileRenderer 
{
	private Map map;
	private RepresentationFactory repFact;
	private static final boolean DRAW_CACHED = true;
	private static final boolean VERBOSE_LOG = false;
	
	/**
	 * Constructor
	 */
	public TileRenderer(Map map)
	{
		this.map = map;
		repFact = RepresentationFactory.getInstance();
	}
	
	public void renderTiles(Graphics g, Tile tiles[][])
	{
		ObjectRepresentor objRep;
		java.util.List<ObjectRepresentor> toDraw;
		Positioner pos = new Positioner();
		Point p;
		Location ref;
		
		ref = new Location(0, 0);
		
		if (DRAW_CACHED)
		{
			toDraw = new java.util.ArrayList<ObjectRepresentor>(1000);
			//toDraw = new java.util.LinkedList<ObjectRepresentor>();
		}
		
		if (VERBOSE_LOG)
			System.out.printf("[VIEW] tiles is %dx%d.\n", tiles.length, tiles[0].length);
		
		for (int i = 0; i < tiles.length; i++)
		{
			for (int j = 0; j < tiles[i].length; j++)
			{
				p = pos.convertLocation(ref, new Location(i, j));
				Tile t = tiles[i][j];
				
				if (t != null)
				{
					// TODO: don't add null items
					
					if (DRAW_CACHED)
					{
						objRep = repFact.get(t.getTerrain());
						objRep.drawPrep(p);
						toDraw.add(objRep);
						objRep = repFact.get(t.getDecal());
						objRep.drawPrep(p);
						toDraw.add(objRep);
						objRep = repFact.get(t.getItem());
						objRep.drawPrep(p);
						toDraw.add(objRep);
						objRep = repFact.get(t.getEntity());
						objRep.drawPrep(p);
						toDraw.add(objRep);
					}
					else
					{
						objRep = repFact.get(t.getTerrain());
						objRep.draw(g, p);
						objRep = repFact.get(t.getDecal());
						objRep.draw(g, p);
						objRep = repFact.get(t.getItem());
						objRep.draw(g, p);
						objRep = repFact.get(t.getEntity());
						objRep.draw(g, p);
					}
				}

			}
		}
		
		if (DRAW_CACHED)
		{
			if (VERBOSE_LOG)
				System.out.printf("[VIEW] toDraw has %d ORs.\n", toDraw.size());
			
			for (ObjectRepresentor or : toDraw)
				or.drawCached(g);
		}
	}
}
